<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\User;
use think\facade\Session;
use think\Request;

class Login
{

    public function login(Request $request){
        $params = $request->param();
        $phone = $params['phone'];
        $newCode = $params['code'];
            $data = User::where('phone',$phone)->find();
            if ($data){
                Session::set('user',$data);
                return json(['code'=>200,'msg'=>'登录成功','data'=>$data]);
            }else{
                $arr['phone'] = $phone;
                $data = User::create($arr);
                return json(['code'=>200,'msg'=>'注册成功','data'=>$data]);
            }
        }

        public function wxlogin(Request $request){
        $params = $request->param();
        $data = User::where('openid',$params['openid'])->find();
            if ($data){
                return json(['code'=>200,'msg'=>'登录成功','data'=>$data]);
            }else{
                $arr['openid'] = $params['openid'];
                $arr['nickname'] = $params['nickname'];
                $arr['pic'] = $params['pic'];
                $data = User::create($arr);
                return json(['code'=>200,'msg'=>'注册成功','data'=>$data]);
            }
        }

        public function wxPromise(Request $request){
        $code = input('code');
        $appid = config('wx.appid');
        $secret = config('wx.secret');
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $arr = $this->curl($url);
        return json(['code'=>200,'data'=>$arr]);
        }

        public function change(Request $request){
           $phone= input('phone');
           $openid = input('openid');
           if (!empty($phone)){
              $flag =  User::where('id',$uid)->update(['phone'=>$phone]);
               if ($flag){
                   return json(['code'=>200,'msg'=>'绑定成功']);
               }
           }
           if (!empty($openid)){
               $params = $request->param();
               $arr['openid'] = $params['openid'];
               $arr['nickname'] = $params['nickname'];
               $arr['pic'] = $params['pic'];
               $uid = $params['uid'];
               $flag =  User::where('id',$uid)->update($arr);
               if ($flag){
                   return json(['code'=>200,'msg'=>'绑定成功']);
               }
           }
        }

        public function curl($url){
            $headerArray =array("Content-type:application/json;","Accept:application/json");
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
            $output = curl_exec($ch);
            curl_close($ch);
            $output = json_decode($output,true);
            return $output;
        }

    public function code(Request $request)
    {
        include_once("SmsSDK.php");
       $to = input('phone');
       $code= mt_rand(1000,9999);
       $datas=[$code,5];
//主帐号
        $accountSid = '8a216da877bef0fe0177dd395b330b51';
//主帐号Token
        $accountToken = '5621f5ab923846538816d515945e6b4a';
//应用Id
        $appId = '8a216da877bef0fe0177dd395c490b57';
//请求地址，格式如下，不需要写https://,默认为：app.cloopen.com'
        $serverIP = 'app.cloopen.com';
//请求端口 默认为：8883
        $serverPort = '8883';
//REST版本号 默认为：2013-12-26
        $softVersion = '2013-12-26';
// 初始化REST SDK
        $rest = new \REST($serverIP, $serverPort, $softVersion);
        $rest->setAccount($accountSid, $accountToken);
        $rest->setAppId($appId);

// 发送模板短信

        $result = $rest->sendTemplateSMS($to, $datas, 1);
        if ($result->statusCode != 0) {
            return json(['code'=>300,'msg'=>'发送失败']);
            //TODO 添加错误处理逻辑
        } else {
            Session::set('code',$code);
            return json(['code'=>200,'msg'=>'发送成功','data'=>$code]);
            //TODO 添加成功处理逻辑
        }
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(Request $request)
    {
       $id = input('id');
       $data =User::find($id);
       return  json(['code'=>200,'data'=>$data]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
